package io.polly.ldz.reactor.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author ldz112@163.com
 * @Classname ElasticSearchClientConfig
 * @Version V1.0
 * @Description 配置类
 * @Date 2024/10/8 15:53
 * Copyright (C) 2023 All Rights Reserved
 * https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/installation.html
 */
@Configuration
public class ElasticSearchClientConfig {

    @Bean
    public ElasticsearchClient elasticsearchClient() {
        // ES服务器URL
        String serverUrl = "http://192.168.150.128:9200";
//        // ES用户名和密码
//        String userName = "xxx";
//        String password = "xxx";

        BasicCredentialsProvider creditProv = new BasicCredentialsProvider();
//        creditProv.setCredentials(
//                AuthScope.ANY, new UsernamePasswordCredentials(userName, password)
//        );

        RestClient restClient = RestClient
                .builder(HttpHost.create(serverUrl))
//                .setHttpClientConfigCallback(hc -> hc.setDefaultCredentialsProvider(creditProv))
                .build();

        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        return new ElasticsearchClient(transport);
    }


}
